

request =require 'request'
uuid =require 'node-uuid'
utf8 = require 'utf8'
crypto = require 'crypto'


arr=[]
arr_key=[]
arr_sort=[]
query_string =''
access_key_id='v99w5K1D0TLXjMDr'
access_key='q085Jj8PcUoM1a83t3GPJmH5Suj4jm'

arr['TimeStamp']=new Date().toISOString()
arr['Format']='JSON'
arr['AccessKeyId']=access_key_id
arr['Action']='DescribeInstances'
arr['SignatureMethod']='HMAC-SHA1'
arr['SignatureNonce']=uuid.v4()
arr['Version']='2014-05-26'
arr['SignatureVersion']='1.0'
arr['RegionId']='cn-beijing'





for key,value of arr
  arr_key.push key

arr_key.sort()

for key,value of arr_key
  key_sort=encodeURIComponent(value)
  arr_sort[key_sort]=encodeURIComponent arr[value]


for key,value of arr_sort
  query_string += (key + '='+value+'&')

query_string= query_string.slice 0,query_string.length-1





stringTOSign= 'GET&%2F&'+encodeURIComponent query_string



hema=crypto.createHmac 'sha1',access_key+'&'
hema.update(stringTOSign)
signature=hema.digest()
signature=signature.toString('base64')


http="http://ecs.aliyuncs.com/?"+"signature="+encodeURIComponent(signature)+"&"
http +=query_string


request http,(error,response,body)->
  if not error and (response.statusCode is 200)
    console.log 'the result',(body)
